package com.tma.mobilechart.utilitys;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Environment;
import android.util.Log;
import android.view.View;

public class ScreenShot {

	public static Bitmap takeScreenShot(Activity activity)
	{
	    View view = activity.getWindow().getDecorView();
	    view.setDrawingCacheEnabled(true);
	    view.buildDrawingCache();
	    Bitmap b1 = view.getDrawingCache();
	    Rect frame = new Rect();
	    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
	    int statusBarHeight = frame.top;	    
	    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
	    int height = activity.getWindowManager().getDefaultDisplay().getHeight();	    
	    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
	    view.destroyDrawingCache();
	    return b;
	}
	public static String savePic(Bitmap b)
	{
		String path = "";
	    FileOutputStream fos = null;
	    File file = new File(Environment.getExternalStorageDirectory(), "/chartsreenshot");
	    if (!file.exists()) {
	        if (!file.mkdirs()) {
	            Log.e("TravellerLog :: ", "Problem creating Image folder");	            
	        }
	    }
	    try
	    {
	    	path = "/sdcard/chartsreenshot/chart" + System.currentTimeMillis() + ".png" ;
	    	fos = new FileOutputStream(path);
        	if ( fos != null ) 
        	{ 
                b.compress(Bitmap.CompressFormat.PNG, 90, fos ); 
                fos.close(); 
        	} 
	    }
	    catch (FileNotFoundException e)
	    {
	        e.printStackTrace();
	    }
	    catch (IOException e)
	    {
	        e.printStackTrace();
	    }
	    return path;
	}	
	
}
